package com.yonyou.service.demo.service;

import com.yonyou.service.demo.exception.APPIdException;
import com.yonyou.service.demo.util.UUID;
import com.yonyou.service.demo.util.Utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.security.InvalidParameterException;

/**
 * App Id 的生成和校验
 * 目前APP ID写死两个，放在配置文件中，其实应该自动生成后，存入数据库
 */
@Service
public class APPIdService {

    @Value("${APPIDS}")
    String appIds;

    public boolean checkAppId(final String appId) throws APPIdException {
        if (Utils.isEmptyString(appId)) throw new InvalidParameterException("APPID is empty");
        String[] appids = appIds.split(",");
        for (String id : appids) {
            if (id.equals(appId)) {
                return true;
            }
        }
        throw new APPIdException("Invalid APPID");
    }

    public String buildAppId() {
        return UUID.uuid().replaceAll("-", "");
    }
}
